package com.teamin.web.controller;


import com.teamin.config.ErrorCode;
import com.teamin.entity.CommentEntity;
import com.teamin.service.FileService;
import com.teamin.service.WebsocketService;
import com.teamin.service.impl.FileServiceImpl;
import com.teamin.service.impl.TaskServiceImpl;
import com.teamin.service.impl.WebsocketServiceImpl;
import com.teamin.web.exception.GlobalErrorInfoException;
import com.teamin.web.model.ErrorInfo;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import uk.co.jemos.podam.api.PodamFactory;
import uk.co.jemos.podam.api.PodamFactoryImpl;

@RestController
@RequestMapping("/v1")
@Api(basePath = "/v1",description = "任务的评论相关",tags = "评论")
public class CommentController extends BaseController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private TaskServiceImpl taskService;
	@Autowired
	private WebsocketServiceImpl websocketService ;
	@Autowired
	private FileService fileService;

	PodamFactory factory = new PodamFactoryImpl();
	/**
	 * 删除评论
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/comments/{commentId}", method = RequestMethod.DELETE)
	public String deleteComments(@PathVariable("commentId") Long commentId,@RequestAttribute("loginUserId") Long loginUserId,
								 @RequestAttribute("userFlag") String userFlag) throws Exception{
		CommentEntity commentEntity = taskService.getCommentById(commentId);
		//不是自己的不能删除
		if (!commentEntity.getUserId().equals(loginUserId)) {
			throw new GlobalErrorInfoException(new ErrorInfo(HttpStatus.BAD_REQUEST, ErrorCode.FORBIDDEN_DELETE_COMMENT));
		}

		if(taskService.deleteCommentById(commentEntity) > 0) {
			//发送任务的同步
			websocketService.sendTask(loginUserId,taskService.getCirclesByTask(commentEntity.getTaskId(),loginUserId).getCircleId(),commentEntity.getTaskId(), WebsocketService.TaskPropertise.COMMENT, WebsocketService.Control.DELETE,commentId,userFlag);
		}
		return "success";
	}

}